﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRCPhase2Tester.Daemons;
using RoutingDaemon.Backend;

namespace IRCPhase2Tester.Tests
{
    class CorrectedLSA : MultiClientsMultiDaemons
    {
        #region ITest Members

        public override string Title()
        {
            return "Corrected LSA";
        }

        public override void BuildUp()
        {
            StartNewDaemon(1, Session.Instance.AllConfigurationFiles[1], typeof(ExternalRoutingDaemon));
            StartNewDaemon(2, Session.Instance.AllConfigurationFiles[2], typeof(LocalRoutingDaemon));

            base.BuildUp();
        }

        public override bool RunTest()
        {
            int attempts = 5;
            while (attempts-- > 0)
            {
                if (DaemonBackEnd.Instance.LocalNode.LastSequenceNumber == 3)
                {
                    daemons[1].Suspend();
                    daemons[1].Kill();
                    daemons.RemoveAt(1);
                    break;
                }
                System.Threading.Thread.Sleep(20000);
            }

            StartNewDaemon(2, Session.Instance.AllConfigurationFiles[2], typeof(LocalRoutingDaemon));

            int sequenceNumber = 1;

            attempts = 5;
            while (sequenceNumber != 3)
            {
                if (DaemonBackEnd.Instance.LocalNode.LastSequenceNumber > sequenceNumber + 1)
                {
                    return true;
                }
                sequenceNumber = DaemonBackEnd.Instance.LocalNode.LastSequenceNumber;
                System.Threading.Thread.Sleep(1000);
            }

            return false;
        }

        public override int NumberOfMinutes()
        {
            return 5;
        }

        #endregion
    }
}
